<!doctype html>
<html lang="en">
 <head> 
  <meta charset="UTF-8"> 
  <meta name="description" content="燎火的个人博客"> 
  <meta name="keyword" content="燎火,qq799378666,软件开发,java,c#,js,html,html5,css"> 
  <title>Java中如何使用嵌入MySQL</title> 
  <script src="/js/jquery-1.11.1.min.js"></script> 
  <script src="/js/bootstrap-3.3.5/js/bootstrap.js"></script> 
  <link href="/js/bootstrap-3.3.5/css/bootstrap.css" rel="stylesheet"> 
  <script src="/js/base.js"></script> 
  <script src="/js/nav.js"></script> 
  <script src="/js/jquery.toTop.min.js"></script> 
  <link href="/style/article.css" rel="stylesheet"> 
 </head> 
 <body> 
  <div id="main"> 
   <h3 id="title">Java中如何使用嵌入MySQL</h3> 
   <div id="content">
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">这篇文件主要介绍在Java中嵌入式MySQL的使用，对于一些的应用项目，提供安装版的Mysql，Oracle是必须的工作。但是有时候如果是一个小的工具，可安装或者移植性比较强的小软件。再去安装数据库可能就比较麻烦了。</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">其实MySQL也有嵌入式的，不需要安装，在使用的过程中，会自动创建数据库以及通过代码的方式启动或者关闭。下面提供一些代码片段，具体的会提供下载地址。</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">这个是核心代码类，这个类实现了Mysql 的启动和停止以及数据库的启动状态。</p>
    <pre class="hljs java" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; overflow-x: auto; padding: 2px; background-color: rgb(63, 63, 63); color: rgb(220, 220, 220); border-radius: 3px; line-height: 1.4; word-wrap: normal; font-size: 13px; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;">package&nbsp;net.simple.mysql;import&nbsp;java.io.File;import&nbsp;java.util.HashMap;import&nbsp;java.util.Map;import&nbsp;java.util.Properties;import&nbsp;java.util.Set;import&nbsp;com.mysql.management.MysqldResource;/**
&nbsp;*&nbsp;
&nbsp;*&nbsp;@author&nbsp;李岩飞
&nbsp;*&nbsp;@email&nbsp;eliyanfei@126.com	
&nbsp;*&nbsp;2016年11月2日&nbsp;下午1:44:55
&nbsp;*
&nbsp;*/public&nbsp;final&nbsp;class&nbsp;EmbedMySqlServer&nbsp;{	private&nbsp;MysqldResource&nbsp;mysqlInstance;	//配置信息
	public&nbsp;final&nbsp;Properties&nbsp;props;	//端口信息
	private&nbsp;String&nbsp;port;	/**
	&nbsp;*&nbsp;考虑到数据库的性能问题,允许将数据库放在其它磁盘
	&nbsp;*/
	private&nbsp;String&nbsp;embedMySqlHome;	public&nbsp;EmbedMySqlServer(final&nbsp;Properties&nbsp;props)&nbsp;{		this.props&nbsp;=&nbsp;props;
	}	public&nbsp;EmbedMySqlServer(final&nbsp;Properties&nbsp;props,&nbsp;String&nbsp;embedMySqlHome)&nbsp;{		this.embedMySqlHome&nbsp;=&nbsp;embedMySqlHome;		this.props&nbsp;=&nbsp;props;
	}	public&nbsp;final&nbsp;String&nbsp;getEmbedMySqlHome()&nbsp;{		return&nbsp;null&nbsp;==&nbsp;embedMySqlHome&nbsp;?&nbsp;getPlatformBaseDir()&nbsp;:&nbsp;embedMySqlHome;
	}	/**
	*&nbsp;获得当前应用主目录
	*&nbsp;@return&nbsp;当前应用启动程序所在目录.
	*/
	public&nbsp;static&nbsp;String&nbsp;getPlatformBaseDir()&nbsp;{		return&nbsp;System.getProperty("user.dir");
	}	public&nbsp;static&nbsp;boolean&nbsp;isBlank(final&nbsp;String&nbsp;str)&nbsp;{		int&nbsp;strLen;		if&nbsp;(str&nbsp;==&nbsp;null&nbsp;||&nbsp;(strLen&nbsp;=&nbsp;str.length())&nbsp;==&nbsp;0)&nbsp;{			return&nbsp;true;
		}		for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;strLen;&nbsp;i++)&nbsp;{			if&nbsp;(Character.isWhitespace(str.charAt(i))&nbsp;==&nbsp;false)&nbsp;{				return&nbsp;false;
			}
		}		return&nbsp;true;
	}	public&nbsp;void&nbsp;startup()&nbsp;{		final&nbsp;File&nbsp;baseDir&nbsp;=&nbsp;new&nbsp;File(getEmbedMySqlHome(),&nbsp;"mysql-em");
		mysqlInstance&nbsp;=&nbsp;new&nbsp;MysqldResource(baseDir);
		port&nbsp;=&nbsp;props.getProperty("port");		if&nbsp;(isBlank(port))
			props.put("port",&nbsp;port&nbsp;=&nbsp;String.valueOf((int)&nbsp;(Math.random()&nbsp;*&nbsp;40000)));		final&nbsp;Set&lt;Object&gt;&nbsp;keys&nbsp;=&nbsp;props.keySet();		final&nbsp;Map&lt;String,&nbsp;String&gt;&nbsp;options&nbsp;=&nbsp;new&nbsp;HashMap&lt;String,&nbsp;String&gt;(keys.size());		for&nbsp;(final&nbsp;Object&nbsp;key&nbsp;:&nbsp;keys)&nbsp;{			final&nbsp;String&nbsp;val&nbsp;=&nbsp;props.getProperty(key.toString());			if&nbsp;("".equals(val))
				options.put(key.toString(),&nbsp;null);			else
				options.put(key.toString(),&nbsp;val.replace("{$contextPath}",&nbsp;getPlatformBaseDir()));
		}		if&nbsp;(!mysqlInstance.isRunning())
			mysqlInstance.start("Em_MySQL",&nbsp;options,&nbsp;false,&nbsp;keys.contains("defaults-file"));
	}	public&nbsp;String&nbsp;getPort()&nbsp;{		return&nbsp;port;
	}	/**
	&nbsp;*&nbsp;判断mysql是否正在运行
	&nbsp;*/
	public&nbsp;boolean&nbsp;isRunning()&nbsp;{		return&nbsp;null&nbsp;==&nbsp;mysqlInstance&nbsp;?&nbsp;false&nbsp;:&nbsp;mysqlInstance.isRunning();
	}	public&nbsp;void&nbsp;shutdown()&nbsp;{		if&nbsp;(mysqlInstance&nbsp;!=&nbsp;null)
			mysqlInstance.shutdown();
	}	public&nbsp;void&nbsp;cleanup()&nbsp;{		if&nbsp;(mysqlInstance&nbsp;!=&nbsp;null)
			mysqlInstance.cleanup();
	}
}</pre>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">下面这个是启动Demo，</p>
    <pre class="hljs cpp" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; overflow-x: auto; padding: 2px; background-color: rgb(63, 63, 63); color: rgb(220, 220, 220); border-radius: 3px; line-height: 1.4; word-wrap: normal; font-size: 13px; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;">public&nbsp;static&nbsp;void&nbsp;main(String[]&nbsp;args)&nbsp;{		try&nbsp;{
			Properties&nbsp;pro&nbsp;=&nbsp;new&nbsp;Properties();			//根据机器配置，设置不同的参数
			pro.load(MysqlTest.class.getResourceAsStream("MySql_medium.properties"));			new&nbsp;EmbedMySqlServer(pro).startup();			//可以把数据库放到其他磁盘
			//new&nbsp;EmbedMySqlServer(pro,"f:\\").startup();
			Connection&nbsp;conn&nbsp;=&nbsp;getTestConnection();
			System.out.println(conn.isClosed());
			conn.close();
		}&nbsp;catch&nbsp;(Exception&nbsp;e)&nbsp;{
			e.printStackTrace();
		}
	}</pre>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">MySql_general.properties一般机器的配置样例</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">MySql_medium.properties中等机器的配置样例</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">MySql_large.properties高配机的配置样例</p>
    <pre class="hljs lua" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; overflow-x: auto; padding: 2px; background-color: rgb(63, 63, 63); color: rgb(220, 220, 220); border-radius: 3px; line-height: 1.4; word-wrap: normal; font-size: 13px; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;">如果需要初始化用户名和密码，在启动的配置文件里面，加入下面3句话
initialize-user=trueinitialize-user.user=root
initialize-user.password=123456</pre>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">具体的参数可以根据不同需求进行定义，比如端口可以自由定义。</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">需要引用的mysql两个jar，mysql-connector-mxj-gpl-6-0-11-db-files.jar，<strong style="box-sizing: inherit; -webkit-tap-highlight-color: transparent;">mysql-connector-mxj-gpl-6-0-11.jar</strong></p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">代码在Git上，地址是：<a href="https://git.oschina.net/eliyanfei/api_tools.git" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; border: none; outline: 0px; transition: color 0.3s; text-decoration: none; color: rgb(68, 102, 187);">https://git.oschina.net/eliyanfei/api_tools.git</a></p>
    <p><br></p>
   </div> 
  </div>  
 </body>
</html>